﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SHJunketingBLL;
using SHJunketingModel;
using Web;

public partial class Admin_UserManagement_AddUserRole : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            GetCbListRole();
        }
    }

    //绑定CheckBoxList
    private void GetCbListRole()
    {
        IList<C_UserRoleInfo> list = C_UserRoleInfoManager.GetAllC_UserRoleInfos();
        this.cbRole.DataSource = list;
        this.cbRole.DataTextField = "RoleName";
        this.cbRole.DataValueField = "RoleId";
        this.cbRole.DataBind();
    }
    //添加
    protected void btnEdit_Click(object sender, EventArgs e)
    {

        string name = this.txtName.Text;

        List<int> roleList = new List<int>();

        foreach (ListItem li in this.cbRole.Items)
        {
            if (li.Selected == true)
            {
                roleList.Add(int.Parse(li.Value));
            }
        }

        //查找用户时候存在
        if (C_UserLoginManager.GetUserIds(name) != null)
        {
            Web.CommFun.Show_Msg("用户已经存在！");
        }
        else
        {
            C_UserRole userRole = new C_UserRole();
            //添加数据到users表
            string sql = "insert into users values('" + txtName.Text.Trim() + "','666666','" + txtUserName.Text.Trim() + "','" + rdoSex.SelectedValue + "','" + txtDepartment.Text.Trim() + "','" + txtCostCenters.Text.Trim() + "','" + txtUserIDCard.Text.Trim() + "','" + txtUserAge.Text.Trim()
                + "','" + rdoMarriage.SelectedValue + "','" + txtPhoneNumber.Text.Trim() + "','" + txtTelephone.Text.Trim() + "','" + txtAddress.Text.Trim() + "','" + txtUserEmai.Text.Trim() + "','" + txtArea.Text.Trim() + "',0,'" + rdoLang.SelectedValue + "','"+rdologinType.SelectedValue+"','" + rdoVIP.SelectedValue + "','" + rdoUser.SelectedValue + "','" + this.txtRemark.Text + "')";

            sql += " ; SELECT @@IDENTITY";

            int userid = DBHelper.GetScalar(sql);

            foreach (int roleId in roleList)
            {
                userRole.UserId = userid;
                userRole.RoleId = roleId;
                C_UserRoleManager.AddC_UserRole(userRole);
            }

            Web.CommFun.Show_Msg("添加成功！", "ProfileUsers.aspx");
        }
    }
}
